<?php

/**
 * Base class for own implementation of form processing.
 * 
 * @package		symfony
 * @subpackage	additional
 * @author		tomek
 */
class RiseBackendFormProcessing
{
	/**
	 * Instance of sfActions object.
	 * 
	 * @var sfActions
	 */
	protected $autoActions;

	/**
	 * warning || notice || error
	 * 
	 * @var string
	 */
	protected $flashType;

	/**
	 * Message text.
	 * 
	 * @var string
	 */
	protected $notice;

	/**
	 * One of instance from propel generated classes.
	 * 
	 * @var RiseObject generated by propel generator
	 */
	protected $riseObject;

	/**
	 * Name of the route that action will be redirect.
	 * 
	 * @var string
	 */
	protected $redirectRoute;


	/**
	 * Class constructor
	 * 
	 * @param	sfActions $autoActions
	 * @return	void
	 */
	public function __construct(sfActions $autoActions)
	{
		$this->autoActions = $autoActions;
		if(!method_exists($autoActions, 'getDispatcher'))
		{
			throw new sfException("autoActions class must have method 'getDispatcher()' which returns dispatcher field!");
		}
	}

	/**
	 * Returns sfActions instance.
	 * 
	 * @return sfActions
	 */
	public function getAutoActions()
	{
		return $this->autoActions;
	}

	/**
	 * In the body of method are defined all operations executed before
	 * form processing.
	 * 
	 * @return void
	 */
	protected function preProcessForm()
	{
	}

	/**
	 * In the body of method are defined all operations executed after
	 * form processing.
	 * 
	 * @return void
	 */
	protected function postProcessForm()
	{
	}

	/**
	 * Method which will be executed in the middle of the processForm method.
	 * 
	 * @param	sfWebRequest $request
	 * @param	sfForm $form
	 * @param	array $params			All params will be transformed to local method variables
	 * @return	void
	 */
	protected function inProcessForm(sfWebRequest $request, sfForm $form, $params = array())
	{
		foreach($params as $var => $param)
		{
			${$var} = $param;
		}
	}

	/**
	 * Executes processForm method with additional functions.
	 * 
	 * After form save runs inProcessForm method.
	 * 
	 * @param	sfWebRequest $request
	 * @param	sfForm $form
	 * @see		cache/backend/dev/modules/auto[Module_name]/actions/actions.class.php
	 * @return	void
	 */
	public function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

		if ($form->isValid())
		{
			$this->notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

			$this->flashType = 'notice';

			$this->riseObject = $form->save();

			if(!$this->inProcessForm($request, $form))
			{
				$this->notice .= ' But the link for this menu is already generated.';
				$this->flashType = 'warning';
			}

			$this->getAutoActions()->getDispatcher()->notify(new sfEvent($this->getAutoActions(), 'admin.save_object', array('object' => $this->riseObject)));

			if ($request->hasParameter('_save_and_add'))
			{
				$this->getAutoActions()->getUser()->setFlash($this->flashType, $this->notice.' You can add another one below.');

				$this->getAutoActions()->redirect('@'.$this->redirectRoute.'_new');
			}
			else
			{
				$this->getAutoActions()->getUser()->setFlash($this->flashType, $this->notice);

				$this->getAutoActions()->redirect(array('sf_route' => $this->redirectRoute.'_edit', 'sf_subject' => $this->riseObject));
			}
		}
		else
		{
			$this->getAutoActions()->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
		}

	}

}